import 'package:flutter/material.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:flutter/cupertino.dart';

class ClickItem extends StatefulWidget {

  ClickItem({
    Key? key,
    this.onTap,
    this.switchTap,
    this.switchState = false,
    required this.title,
    this.content = '',
    this.textAlign = TextAlign.start,
    this.maxLines = 1,
    this.isBorder = true,
    this.contentTextStyle,
  }): super(key: key);

  final GestureTapCallback? onTap;
  final GestureTapCallback? switchTap;
  bool?  switchState;
  final String title;
  final String content;
  final TextAlign textAlign;
  final int maxLines;
  final bool isBorder;
  final TextStyle? contentTextStyle;

  @override
  State<ClickItem> createState() => _ClickItemState();
}

class _ClickItemState extends State<ClickItem> {
  @override
  Widget build(BuildContext context) {
    Widget child = Row(
      //为了数字类文字居中
      crossAxisAlignment: widget.maxLines == 1 ? CrossAxisAlignment.center : CrossAxisAlignment.start,
      children: <Widget>[
        Text(widget.title,style: TextStyle(color: Color(0xFF131415),fontSize: 14,fontFamily: "PingFangSC-Regular"),),
        const Spacer(),
        SizedBox(width: 16),
        Expanded(
          flex: 4,
          child: Text(
            widget.content,
            maxLines: widget.maxLines,
            textAlign: widget.maxLines == 1 ? TextAlign.right : widget.textAlign,
            overflow: TextOverflow.ellipsis,
            style: widget.contentTextStyle??TextStyle(color: Color(0xFF131415),fontSize: 14,fontFamily: "PingFangSC-Semibold"),
          ),
        ),
        if(widget.onTap != null)SizedBox(width: 8),
        Visibility(
          // 无点击事件时，隐藏箭头图标
          visible: widget.onTap == null ? false : true,
          child: Padding(
            padding: EdgeInsets.only(top: widget.maxLines == 1 ? 0.0 : 2.0),
            child: LoadAssetImage("next_icon",width: 7,height: 12,),
          ),
        ),
      ],
    );

    /// 分隔线
    child = Container(
      margin: const EdgeInsets.only(left: 15.0),
      padding: const EdgeInsets.fromLTRB(0, 15.0, 15.0, 15.0),
      constraints: const BoxConstraints(
        maxHeight: double.infinity,
        minHeight: 60.0,
      ),
      width: double.infinity,
      decoration: BoxDecoration(
        border: widget.isBorder?Border(
          bottom: Divider.createBorderSide(context, width: 0.6),
        ):null,
      ),
      child: child,
    );

    return InkWell(
      onTap: widget.onTap,
      child: child,
    );
  }
}

class ClickSwitchItem extends StatefulWidget {

  ClickSwitchItem({
    Key? key,
    this.switchTap,
    this.switchState = false,
    required this.title,
    this.content = '',
    this.textAlign = TextAlign.start,
    this.maxLines = 1,
    this.isBorder = true,
    this.contentTextStyle,
  }): super(key: key);
  final ValueChanged<bool>? switchTap;
  bool?  switchState;
  final String title;
  final String content;
  final TextAlign textAlign;
  final int maxLines;
  final bool isBorder;
  final TextStyle? contentTextStyle;

  @override
  State<ClickSwitchItem> createState() => _ClickSwitchItemState();
}

class _ClickSwitchItemState extends State<ClickSwitchItem> {
  @override
  Widget build(BuildContext context) {
    Widget child = Row(
      //为了数字类文字居中
      crossAxisAlignment: widget.maxLines == 1 ? CrossAxisAlignment.center : CrossAxisAlignment.start,
      children: <Widget>[
        Text(widget.title,style: TextStyle(color: Global.kTheme.textPrimary6,fontSize: 14,fontFamily: Global.myRegularFont),),
        const Spacer(),
        SizedBox(width: 16),
        Expanded(
          flex: 4,
          child: Text(
            widget.content,
            maxLines: widget.maxLines,
            textAlign: widget.maxLines == 1 ? TextAlign.right : widget.textAlign,
            overflow: TextOverflow.ellipsis,
            style: widget.contentTextStyle??TextStyle(color: Global.kTheme.textPrimary6,fontSize: 14,fontFamily: Global.mySemiboldFont),
          ),
        ),
        Padding(
          padding: EdgeInsets.only(right: 0),
          child: Transform.scale(
            scale: 0.8,
            child: CupertinoSwitch(
              value: widget.switchState!,
              activeColor: Global.kTheme.colorPrimary,
              onChanged: widget.switchTap,
            ),
          ),
        ),
      ],
    );

    /// 分隔线
    child = Container(
      margin: const EdgeInsets.only(left: 15.0),
      padding: const EdgeInsets.fromLTRB(0, 15.0, 5.0, 15.0),
      constraints: const BoxConstraints(
        maxHeight: double.infinity,
        minHeight: 60.0,
      ),
      width: double.infinity,
      decoration: BoxDecoration(
        border: widget.isBorder?Border(
          bottom: Divider.createBorderSide(context, width: 0.6),
        ):null,
      ),
      child: child,
    );

    return child;
  }
}
